<?php
/**[广告视图模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-16 12:53:45
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-12-15 14:31:18
 */
class AdViewModel extends ViewModel{

	protected $tableName='Ad';

	public $viewFields  = array(
		'ad'=>array(
			'*',
			'_type'=>'INNER',
		),
		'user'=>array(
			'username','uid',
			'_type'=>'INNER',
			'_on' =>'user.uid=ad.user_uid',
		),
		'position'=>array(
			'psid','position_name',
			'_type'=>'INNER',
			'_on' =>'position.psid=ad.position_psid',
		),
	);

	/**
	 * [get_data 内容列表]
	 * @param  integer $admin [description]
	 * @return [type]         [description]
	 */
	public function get_data($admin=1)
	{
		$where = null;
		if(isset($uid))
		{
			$where['user_uid'] = $uid;
		}
		// 前台只显示审核通过的
		if(!$admin)
		{
			$where['verifystate'] = 2;
		}
		else
		{
			// 审核状态
			switch (I('get.sta',null,'intval')) 
			{
				case 1:
					$verifystate =1;
					break;
				case 2:
					$verifystate =2;
					break;
				default:
					$_GET['sta']=2;
					$verifystate =2;
					break;
			}
			if(isset($verifystate))
			{
				$where['verifystate'] = $verifystate;

			}
		}
		
		// 关键字查找
		$keyword = I('get.search_keyword');

		if($keyword)
		{
			// 类型
			switch (I('get.search_type',null,'intval')) 
			{
				case 1:	//名称
					$where[] = 'name like "%'.$keyword.'%"';
					break;
				case 2: //用户名
					$uid = D('User')->where('username like "%'.$keyword.'%"')->getField('uid');
					$where['user_uid'] = $uid;
					break;
				case 3: //用户名id
					$where['user_uid'] = $keyword;
					break;
			}
		}

		// 位置
		$psid = I('get.psid',null,'intval');
		if($psid)
		{
			$where['position_psid'] = $psid ;
			
		}

		

		// 时间
		$starttime = I('get.search_begin_time');

		if($starttime)
		{
			$where[] ='addtime >='.strtotime($starttime) ;
		}

		$endtime = I('get.search_end_time');
		if($endtime)
		{
			$where[] ='addtime <='.strtotime($endtime) ;
		}

		// 每页记录数
		$pageCount = 20;
		// 每页记录数 后台固定20条一页 ，前台读取分类中的信息，前台必须传递get.cid
		$pageCount = $admin ? $pageCount : $categoryModel->get_field($cid,'page');
		// 排序
		$order = 'ad.sort asc ,aid desc';

		// 总数
		$count = $this->where($where)->count();

		import('ORG.Util.Page');// 导入分页类
		$page = new Page($count,$pageCount);
		$data = $this->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		
		// echo $this->_sql();die;

		if(!$data) return $data;
		foreach($data as $k=>$v)
		{
			switch ($v['verifystate']) 
			{
				case 1:
					$data[$k]['verifystate']='审核中';
					break;
				
				case 2:
					$data[$k]['verifystate']='通过';
					break;
			}
			$data[$k]['addtime']=date('Y-m-d',$v['addtime']);
			$data[$k]['pic']= $v['pic']?__ROOT__.'/'.$v['pic']:__ROOT__.'/Data/Image/default.gif';
		}
		$page->setConfig('theme','<span>%totalRow% %header% %nowPage%/%totalPage% 页 </span>%upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');
		$data['page'] = $page->show();

		// p($data);die;

		return $data;

	}


	/**
	 * [show_ad 显示广告]
	 * @param  [type] $psid [description]
	 * @param  [type] $row  [description]
	 * @return [type]       [description]
	 */
	public function show_ad($psid,$row)
	{
		$where['verifystate'] = 2;  //审核通过
		$where['position_psid'] = $psid;  //广告位置

		// 排序
		$order = 'ad.sort asc ,aid desc';
		$data = $this->where($where)->order($order)->limit($row)->select();

		if(!$data) return $data;
		foreach($data as $k=>$v)
		{
			// 图片
			$data[$k]['pic']= $v['pic']?__ROOT__.'/'.$v['pic']:__ROOT__.'/Data/Image/default.gif';
			//$data[$k]['pic_en']= $v['pic_en']?__ROOT__.'/'.$v['pic_en']:__ROOT__.'/Data/Image/default.gif';
			// 添加时间
			$data[$k]['addtime']=date('Y-m-d',$v['addtime']);
		}

		return $data;
	}


	
}